package com.gzit.view.tag;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.NinePatch;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

import com.gzit.R;

public class TagItem {

	// 自己打的标签的颜色
	public static final int SELF_COLOR = Color.parseColor("#0b439a");
	public static final int SELF_BG_COLOR = Color.WHITE;

	// 别人打的标签的颜色
	public static final int OTHER_COLOR = Color.parseColor("#E4A55B");
	public static final int OTHER_BG_COLOR = Color.parseColor("#FDF1E7");

	// 添加标签颜色
	public static final int FAKE_COLOR = Color.parseColor("#76B640");
	public static final int FAKE_BG_COLOR = Color.parseColor("#C9E39C");

	private Paint selPaint;
	private Paint bgPaint;
	private Paint textPaint;

	private int id;
	private String content;

	private boolean deletable;

	private boolean selectable = true;

	private boolean isSelected;

	private float width;
	private float height;

	private float x;
	private float y;

	private boolean selfFlag;

	// 用户添加标签
	private boolean fake;

	public TagItem(String content) {
		this.content = content;
	}

	// 缓存两个数据，太多的nicepatch占用大量的空间
	static NinePatch blueNP = null;
	static NinePatch orgNP = null;
 
	public void drawNinepath(Canvas c, boolean selFlag, Rect r1,
			TagGroupView pView) {

		if (selFlag) {
			if (blueNP == null) {
				Bitmap bmp = BitmapFactory.decodeResource(pView.getContext()
						.getResources(), R.drawable.bg_blue);
				blueNP = new NinePatch(bmp, bmp.getNinePatchChunk(), null);
			}
			blueNP.draw(c, r1);
			return;
		}

		if (orgNP == null) {
			Bitmap bmp = BitmapFactory.decodeResource(pView.getContext()
					.getResources(), R.drawable.bg_org);
			orgNP = new NinePatch(bmp, bmp.getNinePatchChunk(), null);

		}
		orgNP.draw(c, r1);
	}

	private void drawBg(Canvas canvas, TagGroupView pView) {

		// if (!this.selfFlag) {
		// Drawable self = pView.getContext().getResources()
		// .getDrawable(R.drawable.bg_org);
		// self.setBounds(0, 0, (int) width, (int) height);
		// self.draw(canvas);
		//
		// } else {
		// Drawable other = pView.getContext().getResources()
		// .getDrawable(R.drawable.bg_blue);
		//
		// other.setBounds(0, 0, (int) width, (int) height);
		// other.draw(canvas);
		//
		// }

		drawNinepath(canvas, this.selfFlag, new Rect(0, 0, (int) width,
				(int) height), pView);

		// Paint paint = this.bgPaint;
		//
		// // 画边框
		// if (this.isFake()) {
		// paint.setColor(FAKE_COLOR);
		// PathEffect effects = new DashPathEffect(new float[] { 2, 2, 2, 2 },
		// 1);
		// paint.setPathEffect(effects);
		// } else {
		// paint.setColor(this.selfFlag ? SELF_COLOR : OTHER_COLOR);
		// paint.setPathEffect(null);
		// }
		// paint.setStyle(Style.STROKE);
		// float borderWidth = 2;
		// paint.setStrokeWidth(borderWidth);
		//
		// canvas.drawRect(0, 0, width, height, paint);
		//
		// // 画背景
		// if (this.isFake()) {
		// paint.setColor(FAKE_BG_COLOR);
		// } else {
		// paint.setColor(this.selfFlag ? SELF_BG_COLOR : OTHER_BG_COLOR);
		// }
		//
		// paint.setStyle(Style.FILL);
		// canvas.drawRect(borderWidth, borderWidth, width, height, paint);//
		// 边框两个像素

	}

	private void drawContent(Canvas canvas, TagGroupView pView) {

		if (this.isFake()) {// 添加标签的标签
			this.textPaint.setColor(FAKE_COLOR);
		} else {
			this.textPaint.setColor(this.selfFlag ? SELF_COLOR : OTHER_COLOR);
		}

		canvas.drawText(this.content, this.width / 2, this.height / 2
				+ this.textPaint.getTextSize() / 2 - 1, this.textPaint);

	}

	public boolean containPoint(float x, float y) {
		if (x - this.x > 0 && x - this.x < this.width && y - this.y > 0
				&& y - this.y < this.height) {
			return true;
		}

		return false;

	}

	public void draw(Canvas canvas, TagGroupView pView) {

		canvas.save();
		canvas.translate(x, y);

		this.drawBg(canvas, pView);
		this.drawContent(canvas, pView);
		this.drawDelIcon(canvas, pView);

		canvas.restore();

	}

	private void drawDelIcon(Canvas canvas, TagGroupView pView) {
		// 选中时显示删除图标

		if (pView.isInDeleteMode() && this.isDeletable()) {
			// Paint paint = this.selPaint;
			Drawable delIcon = pView.getDelIconBitmap();

			// 选中时，绘制删除图标
			delIcon.setBounds(getDelIconRect(delIcon));
			delIcon.draw(canvas);

		}

	}

	private Rect getDelIconRect(Drawable delIcon) {
		int iconWidth = delIcon.getIntrinsicWidth();
		int iconHeight = delIcon.getIntrinsicHeight();
		Rect r = new Rect();

		r.left = (int) this.getWidth() - iconWidth / 2;
		r.top = -iconHeight / 2;
		r.right = r.left + iconWidth;
		r.bottom = r.top + iconHeight;

		return r;

	}

	// 节点在删除区域
	public boolean isPointInDeleteArea(float x, float y, Drawable delIcon) {
		if (this.deletable) {
			float x1 = x - this.x;
			float y1 = y - this.y;
			return getDelIconRect(delIcon).contains((int) x1, (int) y1);

		}

		return false;

	}

	public float getWidth() {
		return width;
	}

	public void setWidth(float width) {
		this.width = width;
	}

	public float getHeight() {
		return height;
	}

	public void setHeight(float height) {
		this.height = height;
	}

	public boolean isSelected() {
		return isSelected;
	}

	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;
	}

	public String getContent() {
		return content;
	}

	public boolean isFake() {
		return fake;
	}

	public void setFake(boolean fake) {
		this.fake = fake;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Paint getSelPaint() {
		return selPaint;
	}

	public void setSelPaint(Paint selPaint) {
		this.selPaint = selPaint;
	}

	public Paint getBgPaint() {
		return bgPaint;
	}

	public void setBgPaint(Paint bgPaint) {
		this.bgPaint = bgPaint;
	}

	public Paint getTextPaint() {
		return textPaint;
	}

	public void setTextPaint(Paint textPaint) {
		this.textPaint = textPaint;
	}

	public boolean isDeletable() {
		return deletable;
	}

	public void setDeletable(boolean deletable) {
		this.deletable = deletable;
	}

	public boolean isSelectable() {
		return selectable;
	}

	public void setSelectable(boolean selectable) {
		this.selectable = selectable;
	}

	public boolean isSelfFlag() {
		return selfFlag;
	}

	public void setSelfFlag(boolean selfFlag) {
		this.selfFlag = selfFlag;
	}

	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
	}

	public float getY() {
		return y;
	}

	public void setY(float y) {
		this.y = y;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

}
